package com.paisheng.justlife.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.paisheng.justlife.R;
import com.paisheng.justlife.utils.RegularExpression;
import com.paisheng.justlife.utils.ToastUtil;

import java.lang.reflect.Method;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 回收员登陆页面
 */
public class CollectorActivity extends Activity implements View.OnClickListener {

    private TextView tvBack, tvTime;
    private EditText phoneEt, phonePass;
    private Button btnLogin, one, two, three, four, five, six, seven, eight, nine, zero, delAll, del;
    private Intent intent;
    Timer timer = new Timer();
    private int recLen = 60;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collector);
        initView();
        registerClick();
        tvTime.setText(recLen + "");

    }



    @Override
    protected void onResume() {
        super.onResume();
        timer.schedule(task, 1000, 1000);
    }


    private void initView() {
        tvBack = (TextView) findViewById(R.id.tv_back);
        tvTime = (TextView) findViewById(R.id.tv_time);
        phoneEt = (EditText) findViewById(R.id.phone_et);
        phonePass = (EditText) findViewById(R.id.phone_pass);
        btnLogin = (Button) findViewById(R.id.btn_log_in);
        one = (Button) findViewById(R.id.one);
        two = (Button) findViewById(R.id.two);
        three = (Button) findViewById(R.id.three);
        four = (Button) findViewById(R.id.four);
        five = (Button) findViewById(R.id.five);
        six = (Button) findViewById(R.id.six);
        seven = (Button) findViewById(R.id.seven);
        eight = (Button) findViewById(R.id.eight);
        nine = (Button) findViewById(R.id.nine);
        zero = (Button) findViewById(R.id.zero);
        delAll = (Button) findViewById(R.id.del_all);
        del = (Button) findViewById(R.id.del);
        phoneEt.setSelection(phoneEt.getText().length());
//        phoneEt.setKeyListener(null);
        if (android.os.Build.VERSION.SDK_INT <= 10) {
            phoneEt.setInputType(InputType.TYPE_NULL);
            phonePass.setInputType(InputType.TYPE_NULL);
        } else {
            CollectorActivity.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
            try {
                Class<EditText> cls = EditText.class;
                Method setSoftInputShownOnFocus;
                setSoftInputShownOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
                setSoftInputShownOnFocus.setAccessible(true);
                setSoftInputShownOnFocus.invoke(phoneEt, false);
                setSoftInputShownOnFocus.invoke(phonePass, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void registerClick() {
        tvBack.setOnClickListener(this);
        btnLogin.setOnClickListener(this);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        zero.setOnClickListener(this);
        delAll.setOnClickListener(this);
        del.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                startActivity(new Intent(this, MainActivity.class));
                break;
            case R.id.btn_log_in:  //回收员登陆
                String phoneNum = phoneEt.getText().toString();
                String pass = phonePass.getText().toString();
                if(phoneNum.equals("")){
                    ToastUtil.showLong(this, "请输入登录账号");
                    break;
                }
                if(pass.equals("")){
                    ToastUtil.showLong(this, "请输入登录密码");
                    break;
                }

                if(pass.length()<6 || pass.length()>20){
                    ToastUtil.showLong(this, "请输入正确密码");
                    break;
                }

                if (RegularExpression.isMobileNO(phoneNum)) {

                    intent = new Intent(this, PickupTypeActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtil.showLong(this, "手机号码格式不正确！");
                }
                break;
            case R.id.one:
                setNumber(view,"1");

                break;
            case R.id.two:
                setNumber(view,"2");
                break;
            case R.id.three:
                setNumber(view,"3");
                break;
            case R.id.four:
                setNumber(view,"4");
                break;
            case R.id.five:
                setNumber(view,"5");
                break;
            case R.id.six:
                setNumber(view,"6");
                break;
            case R.id.seven:
                setNumber(view,"7");
                break;
            case R.id.eight:
                setNumber(view,"8");
                break;
            case R.id.nine:
                setNumber(view,"9");
                break;
            case R.id.zero:
                setNumber(view,"0");
                break;
            case R.id.del:
                setNumber(view,"del");
                break;
            case R.id.del_all:
                setNumber(view,"del_all");
                break;


        }
    }

    private void setNumber(View view, String s) {
        view = getWindow().getDecorView().findFocus();
        if (view instanceof EditText) {
            if(s.equals("del")){
                Editable text = ((EditText) view).getText();
                if( text.toString().length() == 0){
                    return;
                }
                int index = ((EditText) view).getSelectionStart();
                text.delete(index - 1, index);
                return;
            }else if(s.equals("del_all")){
                ((EditText) view).setText("");
            }else{
                ((EditText) view).append(s);
            }
        }
    }

    public TimerTask task = new TimerTask() {
        @Override
        public void run() {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    recLen--;
                    tvTime.setText("" + recLen);
                    if (recLen <= 0) {
//                        startActivity(new Intent(CollectorActivity.this, MainActivity.class));
//                        timer.cancel();
                    }
//                    Log.e("zpan","======remainTime=====" + recLen);

                }
            });

        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        timer.cancel();
        ToastUtil.cancelToast();
        finish();
    }


}
